package top.ruanlitao.shortlink.project.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import top.ruanlitao.shortlink.project.dao.entity.LinkAccessStatsDO;
import top.ruanlitao.shortlink.project.dao.entity.ShortLinkDO;
import top.ruanlitao.shortlink.project.dto.req.ShortLinkStatsReqDTO;

import java.util.List;

/**
 * 短链接基础访问监控持久层
 */
public interface LinkAccessStatsMapper extends BaseMapper<LinkAccessStatsDO> {

    /**
     * 插入 或者 更新 数据
     * 记录基础访问监控数据（对于一个短链接，一个小时生成一条记录）
     * 通过 full_short_url、date、hour 构成一个唯一索引
     * 当唯一值 (full_short_url、date、hour) 不冲突时，正常插入数据，当唯一值发生冲突时，更新数据
     */
    @Insert("""
                INSERT INTO t_link_access_stats (full_short_url, date, pv, uv, uip, hour, weekday, create_time, update_time, del_flag)
                VALUES ( #{linkAccessStats.fullShortUrl}, #{linkAccessStats.date}, #{linkAccessStats.pv}, #{linkAccessStats.uv}, #{linkAccessStats.uip}, #{linkAccessStats.hour}, #{linkAccessStats.weekday}, NOW(), NOW(), 0 )
                ON DUPLICATE KEY UPDATE pv = pv +  #{linkAccessStats.pv}, uv = uv + #{linkAccessStats.uv}, uip = uip + #{linkAccessStats.uip}, update_time = NOW();
            """)
    void shortLinkStats(@Param("linkAccessStats") LinkAccessStatsDO linkAccessStatsDO);


    /**
     * 根据短链接获取指定日期内基础监控数据
     */
    @Select("""
                SELECT
                    tlas.date,
                    SUM(tlas.pv) AS pv,
                    SUM(tlas.uv) AS uv,
                    SUM(tlas.uip) AS uip
                FROM
                    t_link tl INNER JOIN
                    t_link_access_stats tlas ON tl.full_short_url = tlas.full_short_url
                WHERE
                    tlas.full_short_url = #{param.fullShortUrl}
                    AND tl.gid = #{param.gid}
                    AND tl.del_flag = '0'
                    AND tl.enable_status = #{param.enableStatus}
                    AND tlas.date BETWEEN #{param.startDate} AND #{param.endDate}
                GROUP BY
                    tlas.full_short_url, tl.gid, tlas.date
            """)
    List<LinkAccessStatsDO> listStatsByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);

    /**
     * 根据短链接获取指定日期内小时基础监控数据
     */
    @Select("""
                SELECT
                    tlas.hour,
                    SUM(tlas.pv) AS pv
                FROM
                    t_link tl INNER JOIN
                    t_link_access_stats tlas ON tl.full_short_url = tlas.full_short_url
                WHERE
                    tlas.full_short_url = #{param.fullShortUrl}
                    AND tl.gid = #{param.gid}
                    AND tl.del_flag = '0'
                    AND tl.enable_status = #{param.enableStatus}
                    AND tlas.date BETWEEN #{param.startDate} AND #{param.endDate}
                GROUP BY
                    tlas.full_short_url, tl.gid, tlas.hour
            """)
    List<LinkAccessStatsDO> listHourStatsByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);

    /**
     * 根据短链接获取指定日期内一周的基础监控数据
     */
    @Select("""
                SELECT
                    tlas.weekday,
                    SUM(tlas.pv) AS pv
                FROM
                    t_link tl INNER JOIN
                    t_link_access_stats tlas ON tl.full_short_url = tlas.full_short_url
                WHERE
                    tlas.full_short_url = #{param.fullShortUrl}
                    AND tl.gid = #{param.gid}
                    AND tl.del_flag = '0'
                    AND tl.enable_status = #{param.enableStatus}
                    AND tlas.date BETWEEN #{param.startDate} AND #{param.endDate}
                GROUP BY
                    tlas.full_short_url, tl.gid, tlas.weekday
            """)
    List<LinkAccessStatsDO> listWeekdayStatsByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);


    List<ShortLinkDO> getTodayAndTotalBaseStats(
            @Param("list") List<String> list,
            @Param("orderTag") String orderTag
    );
}
